package com.thinkcube.edutainment.fragment;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.ForumPostAdapter;
import com.thinkcube.edutainment.adapters.ForumPostDetailAdapter;
import com.thinkcube.edutainment.adapters.OtherForumPostAdapter;
import com.thinkcube.edutainment.client.ForumClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.Forum;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.data.OtherForumData;
import com.thinkcube.edutainment.domain.data.PostData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment(R.layout.other_forum_fragment)
public class OtherForumFragment extends Fragment implements OnItemClickListener{

	OtherForumData otherForum;

	CourseData courseData;
	
	boolean  isFromDashBord;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey("Course")) {
			courseData = getArguments().getParcelable("Course");

			System.out.println("COURSE ID:::::" + courseData.getCourseId());
		}

		if (getArguments().containsKey("OtherFourumId")) {
			otherForum = getArguments().getParcelable("OtherFourumId");

			System.out.println("OFID:::::" + otherForum.getId());
		}
		
		if(getArguments().containsKey("fromDashBord"))
		{
			isFromDashBord=getArguments().getBoolean("fromDashBord");
		}
	}

	@Bean
	OAuthDto oAuthDto;

	@RestService
	UserClient userClient;

	@RestService
	ForumClient forumClient;

	@ViewById(R.id.progressBar)
	ProgressBar progress;

	@Pref
	SPreferences_ pref;

	@Bean
	ForumPostAdapter forumPostAdapter;
	@Bean
	OtherForumPostAdapter otherForumPostAdapter;

	@AfterViews
	void initUi() {
		gettoken();

	}

	@Trace
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					System.setProperty("http.keepAlive", "false");
					getForum(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	@Bean
	CourseDto courseDto;

	@Background
	void getForum(OAuth requestToken) {

		courseDto.setAuthToken(requestToken.getoAuth().getToken());

		// courseDto.setUserId(pref.getId().get());

		courseDto.setCourseId(courseData.getCourseId());

		courseDto.setForumId(otherForum.getId());

		// courseDto.setUserId("5289cf1e6bf841e116dd886d");

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					getForumUI(forumClient.getForum(courseDto
							.getMultiValuedMap()));

				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

	}

	@Bean
	ForumPostDetailAdapter forumPostDetailAdapter;

	@ViewById(R.id.oforum_title)
	TextView forumDetailTitle;

	@ViewById(R.id.other_forum_list)
	ListView otherForumList;

	@Trace
	@UiThread
	void getForumUI(Forum forum) {


		forumDetailTitle.setText(forum.getForumData().getTitle());

		forumData=forum.getForumData().getPostDataList();
		forumPostAdapter.setPostData(forumData);

		otherForumList.setAdapter(forumPostAdapter);
		
		progress.setVisibility(View.GONE);
		
		otherForumList.setOnItemClickListener(this);

	}

	
	List<PostData> forumData;
	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
		
		FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
	
		ForumDetailFragment forumFragment = new ForumDetailFragment_();
		Bundle fbundle = new Bundle();
		fbundle.putParcelable("Course", courseData);
		fbundle.putParcelable("FourumId", forumData.get(position));
		forumFragment.setArguments(fbundle);
		
		
		if(isFromDashBord)
		{
			fragmentTransaction.replace(R.id.pane, forumFragment);
		}
		else{
			
			fragmentTransaction.replace(R.id.lesson_content, forumFragment);
		}
		//fragmentTransaction.replace(R.id.lesson_content, forumFragment);
		fragmentTransaction.addToBackStack(null);
		fragmentTransaction.commit();
		
	}

}
